home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / Sources / FWGXUtil.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  9.6 KB  |  358 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWGXUtil.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWGXCFG_H
  13. #include "FWGXCfg.h"
  14. #endif
  15.  
  16. #ifdef FW_SUPPORT_GX
  17.  
  18. #ifndef FWGXUTIL_H
  19. #include "FWGXUtil.h"
  20. #endif
  21.  
  22. #ifndef FWACQUIR_H
  23. #include "FWAcquir.h"
  24. #endif
  25.  
  26. #ifndef FWDEBUG_H
  27. #include "FWDebug.h"
  28. #endif
  29.  
  30. #ifndef FWPOINT_H
  31. #include "FWPoint.h"
  32. #endif
  33.  
  34. #ifndef FWRECT_H
  35. #include "FWRect.h"
  36. #endif
  37.  
  38. // ----- Platform includes
  39.  
  40. #ifndef __GESTALT__
  41. #include <Gestalt.h>
  42. #endif
  43.  
  44. #ifndef __GXGRAPHICS__
  45. #include <GXGraphics.h>
  46. #endif
  47.  
  48. #ifndef __GXPRINTING__
  49. #include <GXPrinting.h>
  50. #endif
  51.  
  52. // ----- OpenDoc includes
  53.  
  54. #ifndef SOM_ODFrame_xh
  55. #include <Frame.xh>
  56. #endif
  57.  
  58. #ifndef SOM_ODFacet_xh
  59. #include <Facet.xh>
  60. #endif
  61.  
  62. #ifndef SOM_ODCanvas_xh
  63. #include <Canvas.xh>
  64. #endif
  65.  
  66. #ifndef SOM_ODShape_xh
  67. #include <Shape.xh>
  68. #endif
  69.  
  70. // ------ Standard includes
  71.  
  72. // #include <string.h>
  73.  
  74. //----------------------------------------------------------------------------------------
  75.  
  76. #ifdef FW_BUILD_MAC
  77. #pragma segment FW_GXUtilities
  78. #endif
  79.  
  80. //----------------------------------------------------------------------------------------
  81. // FW_IsGXInstalled
  82. //----------------------------------------------------------------------------------------
  83.  
  84. FW_Boolean FW_IsGXInstalled()
  85. {
  86.     ODSLong    version;
  87.     static    FW_Boolean alreadyChecked = FALSE, answer = FALSE;
  88.     
  89.     // since this may be called repeatedly, check Gestalt only
  90.     // once and cache the answer.  We'll assume for now that GX
  91.     // doesn't go on an off line during a single session.
  92.     if (alreadyChecked)                // have we already checked Gestalt?        
  93.     {
  94.         return answer;                // just return the same answer
  95.     }
  96.     
  97.     // go and check Gestalt
  98.     alreadyChecked    = true;
  99.     if (Gestalt(gestaltGraphicsVersion, &version) == noErr)
  100.     {
  101.         answer    = true;
  102.         return answer;
  103.     }
  104.     
  105.     answer    = false;
  106.     return answer;
  107. }
  108.  
  109. //----------------------------------------------------------------------------------------
  110. // FW_GX_Initialize
  111. //----------------------------------------------------------------------------------------
  112.  
  113. void            FW_Priv_GX_Initialize()
  114. {
  115.     if (FW_IsGXInstalled())
  116.     {
  117.         ::GXEnterGraphics();
  118.         ::GXInitPrinting();
  119.     }
  120. }
  121.  
  122. //----------------------------------------------------------------------------------------
  123. // FW_GX_Terminate
  124. //----------------------------------------------------------------------------------------
  125.  
  126. void            FW_Priv_GX_Terminate()
  127. {
  128.     if (FW_IsGXInstalled())
  129.     {
  130.         ::GXExitPrinting();
  131.         ::GXExitGraphics();
  132.     }
  133. }
  134.  
  135. //========================================================================================
  136. // class FW_CGraphicContextGX
  137. //========================================================================================
  138.  
  139. FW_DEFINE_AUTO(FW_CGraphicContextGX)
  140.  
  141. //----------------------------------------------------------------------------------------
  142. // FW_CGraphicContextGX::FW_CGraphicContextGX
  143. //----------------------------------------------------------------------------------------
  144.  
  145. FW_CGraphicContextGX::FW_CGraphicContextGX(
  146.         Environment* ev,
  147.         ODFacet* facet,
  148.         ODShape* invalidShape)
  149. {
  150.     FW_ASSERT(FW_IsGXInstalled());
  151.  
  152.     ODCanvas* canvas = facet->GetCanvas(ev);
  153.  
  154.     // ----- Get / create the GX objects that we will need
  155.     fGXTransform = ::GXNewTransform();
  156.     fGXViewPort = (gxViewPort) canvas->GetGXViewport(ev); 
  157.  
  158.     // ----- Set up transform mapping
  159.     SetupTransformMapping(ev, facet);
  160.  
  161.     // ----- Set up viewport clipping
  162.     SetupViewPortClipping(ev, facet, invalidShape);
  163.  
  164.     // ----- Set up transform clipping
  165.     SetupTransformClipping(ev, facet);
  166.  
  167.     // ----- Plug the viewport into the frame's transform
  168.     ::GXSetTransformViewPorts(fGXTransform, 1, &fGXViewPort);
  169.  
  170.     FW_END_CONSTRUCTOR
  171. }
  172.  
  173. //----------------------------------------------------------------------------------------
  174. // FW_CGraphicContextGX::~FW_CGraphicContextGX
  175. //----------------------------------------------------------------------------------------
  176.  
  177. FW_CGraphicContextGX::~FW_CGraphicContextGX()
  178. {
  179.     FW_START_DESTRUCTOR
  180.     
  181.     ::GXSetTransformViewPorts(fGXTransform, 0, NULL);
  182.  
  183.     RestoreTransformClipping();
  184.  
  185.     RestoreViewPortClipping();
  186.  
  187.     RestoreTransformMapping();
  188.     
  189.     ::GXDisposeTransform(fGXTransform);
  190. }
  191.  
  192. //----------------------------------------------------------------------------------------
  193. // FW_CGraphicContextGX::SetupTransformMapping
  194. //----------------------------------------------------------------------------------------
  195.  
  196. void FW_CGraphicContextGX::SetupTransformMapping(
  197.         Environment* ev,
  198.         ODFacet* facet)
  199. {
  200.     ODCanvas* canvas = facet->GetCanvas(ev);
  201.     FW_CAcquiredODTransform aqFacetXForm = facet->AcquireFrameTransform(ev, canvas);
  202.  
  203.     aqFacetXForm->GetMatrix(ev, (ODMatrix*) &fMapping);
  204.  
  205.     ::GXGetTransformMapping(fGXTransform, &fOldMapping);
  206.  
  207.     ::MapMapping(&fMapping, &fOldMapping);
  208.     ::GXSetTransformMapping(fGXTransform, &fMapping);
  209. }
  210.  
  211. //----------------------------------------------------------------------------------------
  212. // FW_CGraphicContextGX::RestoreTransformMapping
  213. //----------------------------------------------------------------------------------------
  214.  
  215. void FW_CGraphicContextGX::RestoreTransformMapping()
  216. {
  217.     ::GXSetTransformMapping(fGXTransform, &fOldMapping);
  218. }
  219.  
  220. //----------------------------------------------------------------------------------------
  221. // FW_CGraphicContextGX::SetupTransformClipping
  222. //----------------------------------------------------------------------------------------
  223.  
  224. void FW_CGraphicContextGX::SetupTransformClipping(
  225.         Environment* ev,
  226.         ODFacet* facet)
  227. {
  228.     gxShape transformClip = NULL;
  229.  
  230.     ODFrame* frame = facet->GetFrame(ev);
  231.     ODCanvas* canvas = facet->GetCanvas(ev);
  232.  
  233.     if (canvas->HasPlatformPrintJob(ev, kODQuickDrawGX))
  234.     {
  235.         // Printing
  236.         FW_CPoint extent;
  237.         frame->GetContentExtent(ev, (ODPoint*)&extent);
  238.         
  239.         FW_CRect extentRect(FW_kZeroPoint, extent);
  240.         transformClip = GXNewRectangle((gxRectangle*)&extentRect); 
  241.     }
  242.     else
  243.     {
  244.         // Drawing to the screen
  245.         FW_CAcquiredODShape aqFrameShape = frame->AcquireFrameShape(ev, NULL);
  246.         transformClip = aqFrameShape->GetGXShape(ev);
  247.     }
  248.  
  249.     fTransformClipOld = ::GXGetTransformClip(fGXTransform);
  250.     ::GXSetTransformClip(fGXTransform, transformClip);
  251.  
  252. #ifdef FW_DEBUG
  253.     gxRectangle boundsTransformClip;
  254.     ::GXGetShapeBounds(transformClip, 0, &boundsTransformClip);
  255.  
  256.     gxRectangle boundsTransformClipOld;
  257.     ::GXGetShapeBounds(fTransformClipOld, 0, &boundsTransformClipOld);
  258. #endif
  259.  
  260.     ::GXDisposeShape(transformClip);
  261. }
  262.  
  263. //----------------------------------------------------------------------------------------
  264. // FW_CGraphicContextGX::RestoreTransformClipping
  265. //----------------------------------------------------------------------------------------
  266.  
  267. void FW_CGraphicContextGX::RestoreTransformClipping()
  268. {
  269.     ::GXSetTransformClip(fGXTransform, fTransformClipOld);
  270.     ::GXDisposeShape(fTransformClipOld);
  271. }
  272.  
  273. //----------------------------------------------------------------------------------------
  274. // FW_CGraphicContextGX::SetupViewPortClipping
  275. //----------------------------------------------------------------------------------------
  276.  
  277. void FW_CGraphicContextGX::SetupViewPortClipping(
  278.         Environment* ev,
  279.         ODFacet* facet,
  280.         ODShape* invalidShape)
  281. {
  282.     ODCanvas* canvas = facet->GetCanvas(ev);
  283.     FW_CAcquiredODShape aqFacetClipShape = facet->AcquireAggregateClipShape(ev, canvas);
  284.  
  285. #ifdef FW_DEBUG
  286.     FW_CRect boundsFacetClipShape;
  287.     aqFacetClipShape->GetBoundingBox(ev, (ODRect*) &boundsFacetClipShape);
  288. #endif
  289.  
  290.     // ----- Map the clip shape's copy back to local coordinates
  291.     gxShape    facetGXShape = aqFacetClipShape->GetGXShape(ev);
  292.     gxShape    viewPortClip = ::GXCopyToShape(NULL, facetGXShape);
  293.     ::GXDisposeShape(facetGXShape);
  294.  
  295.     ::GXSetShapeAttributes(viewPortClip, GXGetShapeAttributes(viewPortClip) & ~gxMapTransformShape);
  296. //    ::GXMapShape(viewPortClip, &fMapping);
  297.  
  298.     // ----- Intersect the clip shape with the invalid  shape
  299.     if (invalidShape != NULL)
  300.     {
  301.         gxShape invalidGXShape = invalidShape->GetGXShape(ev);
  302.         ::GXIntersectShape(viewPortClip, invalidGXShape);
  303.         ::GXDisposeShape(invalidGXShape);
  304.     }
  305.     
  306.     //    [HLX] move here because of bug #1306478
  307.     ::GXMapShape(viewPortClip, &fMapping);
  308.  
  309.     // ----- Save the current clip and set the new old 
  310.     fViewPortClipOld = ::GXGetViewPortClip(fGXViewPort);
  311.  
  312. #ifdef FW_DEBUG
  313.     gxRectangle boundsViewPortClip;
  314.     ::GXGetShapeBounds(viewPortClip, 0, &boundsViewPortClip);
  315.  
  316.     gxRectangle boundsViewPortClipOld;
  317.     ::GXGetShapeBounds(fViewPortClipOld, 0, &boundsViewPortClipOld);
  318. #endif
  319.  
  320.     ::GXSetViewPortClip(fGXViewPort, viewPortClip);
  321.     ::GXDisposeShape(viewPortClip);
  322. }
  323.  
  324. //----------------------------------------------------------------------------------------
  325. // FW_CGraphicContextGX::RestoreViewPortClipping
  326. //----------------------------------------------------------------------------------------
  327.  
  328. void FW_CGraphicContextGX::RestoreViewPortClipping()
  329. {
  330.     ::GXSetViewPortClip(fGXViewPort, fViewPortClipOld);
  331.     ::GXDisposeShape(fViewPortClipOld);
  332. }
  333.  
  334. //----------------------------------------------------------------------------------------
  335. // FW_CGraphicContextGX::DrawShape
  336. //----------------------------------------------------------------------------------------
  337.  
  338. void FW_CGraphicContextGX::DrawShape(gxShape shape)
  339. {
  340.     gxTransform oldTransform = ::GXGetShapeTransform(shape);
  341.     ::GXSetShapeTransform(shape, fGXTransform);
  342.     
  343.     ::GXDrawShape(shape);
  344.  
  345.     ::GXSetShapeTransform(shape, oldTransform);
  346. }
  347.  
  348. //----------------------------------------------------------------------------------------
  349. // FW_CGraphicContextGX::GetGXTransform
  350. //----------------------------------------------------------------------------------------
  351.  
  352. gxTransform FW_CGraphicContextGX::GetGXTransform() const
  353. {
  354.     return fGXTransform;
  355. }
  356.  
  357. #endif    // FW_SUPPORT_GX
  358.